package br.com.searchpeople.service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import br.com.searchpeople.database.SingletonPostBD;
import br.com.searchpeople.model.PostModel;
import android.app.IntentService;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Base64;
import android.util.Log;

public class SearchPeopleService extends IntentService{

	public SearchPeopleService() {
		super("RSSPOST");
		// TODO Auto-generated constructor stub
	}
	
	@Override
	protected void onHandleIntent(Intent intent) {
		//while (true) {
			
			Log.d("JSON", "Service executando");
			DefaultHttpClient httpClient = new DefaultHttpClient();
			HttpGet get = new HttpGet("http://10.0.2.2:3000/posts");

			ResponseHandler<String> responseHandler = new BasicResponseHandler();
			String response = null;
			List<PostModel> listaPostServer = new ArrayList<PostModel>();
			List<PostModel> listaPostApp = new ArrayList<PostModel>();
			List<PostModel> listToAdd = new ArrayList<PostModel>();
			try {
				response = httpClient.execute(get, responseHandler);
				JSONArray jsonArray = new JSONArray(response);
				for (int i = 0; i < jsonArray.length(); i++) {
					JSONObject jsonObject = jsonArray.getJSONObject(i);
					String name = jsonObject.getString("name");
					String description = jsonObject.getString("description");
					String state = jsonObject.getString("state");
					String country = jsonObject.getString("country");
					String city = jsonObject.getString("city");
					String phone = jsonObject.getString("phone");
					String email = jsonObject.getString("email");
					String image = jsonObject.getString("image");
					double latitude = jsonObject.getDouble("latitude");
					double longitude = jsonObject.getDouble("longitude");
					int user_id = jsonObject.getInt("user_id");
					Bitmap stringToBitmap = StringToBitMap(image);
					PostModel postM = new PostModel(name, description, country, state, city, phone, email, stringToBitmap, latitude, longitude, user_id);
					listaPostServer.add(postM);
				}
				SingletonPostBD.getInstance(SearchPeopleService.this).deleteAllPosts();
				for (int i = 0; i < listaPostServer.size();i++) {
					Log.d("image", listaPostServer.get(i).getImg()+"");
					SingletonPostBD.getInstance(SearchPeopleService.this).addPost(listaPostServer.get(i));
				}
				
			} catch (ClientProtocolException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			//stopSelf();
			synchronized (this) {
				try {
					wait(5000);
				} catch (Exception e) {
				}
			}
			
//		}
				
	}
	public Bitmap StringToBitMap(String encodedString){
	     try{
	       byte [] encodeByte = Base64.decode(encodedString,Base64.DEFAULT);
	       Bitmap bitmap = BitmapFactory.decodeByteArray(encodeByte, 0, encodeByte.length);
	       return bitmap;
	     }catch(Exception e){
	       e.getMessage();
	       return null;
	     }
	 }

}
